﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.Core.LogFourNet;

namespace ZhouYu.NewWorld.Web.Attributes
{
    public class LogExceptionFilter : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled)
            {
                string controllerName = (string)filterContext.RouteData.Values["controller"];
                string actionName = (string)filterContext.RouteData.Values["action"]; 
                string msgTemplate = "在执行 controller[{0}] 的 action[{1}] 时产生异常:[{2}]";

                log4net.ILog log = log4net.LogManager.GetLogger("logerror");//获取一个日志记录器
                log.Error(DateTime.Now.ToString() + ": login success");//写入一条新log

                LogHelper.Error(string.Format(msgTemplate, controllerName, actionName, filterContext.Exception.Message), filterContext.Exception);

                filterContext.HttpContext.Response.Redirect("/Error/Index");//自定义报错提示页面
                return;
            }
            else
            {
                base.OnException(filterContext);
            }
        }
    }
}